<?php
namespace Admin\Controller;
use Think\Controller;
class AdminController extends Controller {
    //登录
    public function login(){
        if(!IS_POST){
            $this->display();
        }else {
            $admin = D('Admin');
            $info = $admin->where('admin_name=' . "'" . I('post.admin_name') . "'")->find();
            if(empty($info)){
                $this->error('用户名不存在');
            }else{
                if(md5(I('post.password').$info['salt'])==$info['password']){
                    cookie('admin',$admin->admin_name);
                    cookie('verify',md5($admin->admin_name.C('salt')));
                    $this->redirect('Admin/Blog/clist');
                }else{
                    echo "密码错误";
                }
            }
                
            }
        }
    //注册
    public function register(){
        if(!IS_POST){
            $this->display();
        }else{
            $admin = D('Admin');
            if(!$admin->create()){
                exit($admin->getError());
            }else{
                $yan = $this->salt(); 
                $admin->password = md5(I('post.password').$yan);
                $admin->salt =$yan;
                if($admin->add()){
                   echo '注册成功'; 
                    $this->redirect('Admin/Admin/login');

                }else{
                    $this->error('注册失败');
                }
            }
        }
    }
    
    //退出
    public function logout(){
        cookie('admin',null);
        cookie('verify',null);
        $this->redirect('Admin/Admin/login');
    }
    public function salt(){
       return mt_rand(1000,9999);
    }
    
}